﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers{
public class MenuRolesController : MenuRolesControllerBase{
	public MenuRolesController()
		: base()
	{
	}	
	private DataTable _Table;
	private MenuRolesEntity oMenuRoles;	
	public MenuRolesEntity MenuRolesObject{
		get{
			return oMenuRoles;
		}
		set{
			oMenuRoles=value;
		}
	}
	public DataTable Table{
		get{
			if(_Table==null&&AllowCaching){				
				_Table=Cache.Get<DataTable>(CACHE_KEY_DS);
			}
			return _Table;
		}
		set{
			_Table=value;
		}
	}
	public void LoadAllDSMenuRoles(bool clearCache){
		bool bForce=clearCache;
		if(bForce||_Table==null)
		{
			DataSet ds=Service.SelectAllDS();
			_Table=ds.Tables[0];
			AddIndexToTable(_Table);
			if(AllowCaching)
				AddCache(CACHE_KEY_DS,_Table);			
		}				
	}	
	public void AddIndexToTable(){
		AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		ResetTableIndex(_Table);
	}
	public void LoadObject(Int32 MenuRoleId){
		MenuRolesEntity oLoad=FindMenuRoles(MenuRoleId);
		if(oLoad==null){
			LoadAllMenuRoles(true);
			oMenuRoles=FindMenuRoles(MenuRoleId);
		}
		else{
			oMenuRoles=oLoad;
		}
	}
	public bool AddNew(){
		if(oMenuRoles==null)return false;
		bool result = Service.Save(oMenuRoles);
		if(result){
			AddObjectToCache(oMenuRoles);
		}
		return result;
	}
	public bool Update(){
		if(oMenuRoles==null)return false;
		return Service.Update(oMenuRoles);	
	}
	public bool Delete(){
		if(oMenuRoles==null)return false;		
		bool retvalue = Service.Delete(oMenuRoles);
		if(retvalue){			
			RemoveObjectFromCache(oMenuRoles);
		}
		return retvalue;
	}	
	public bool DeleteByID(Int32 MenuRoleId){
		bool retvalue = Service.DeleteByID(MenuRoleId);		
		if(retvalue){
			MenuRolesEntity oDeleted=FindMenuRoles(MenuRoleId);
			RemoveObjectFromCache(oDeleted);
		}
		return retvalue;
	}	
	public void ResetObject(){
		this.oMenuRoles = new MenuRolesEntity();
	}
	public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.MenuRolesObject,row);
    }
    public MenuRolesEntity LoadMenuRoles(Int32 MenuRoleId){			
		MenuRolesEntity o=Service.SelectOne(MenuRoleId);		
		return o;
	}
	#region Cached
    private static List<MenuRolesEntity>_ListMenuRoles;
	private static bool _AllowCaching=true;
	public static bool AllowCaching{
		get{return _AllowCaching;}
		set{AllowCaching=value;}
	}	
	public static void ClearCache(){
			if(!_AllowCaching)return;
			Cache.Remove(CACHE_KEY_DS);
			Cache.Remove(CACHE_KEY_LS);
	}
	private const string CACHE_KEY_DS="MenuRolesDS";
	private const string CACHE_KEY_LS="MenuRolesLS";
	private void AddObjectToCache(MenuRolesEntity newObject){
		if(!AllowCaching)return;
		GetCacheList().Add(newObject);
	}
	private void RemoveObjectFromCache(MenuRolesEntity oRemove)
	{
		if(!AllowCaching)return;
		if(oRemove!=null)
		GetCacheList().Remove(oRemove);
	}
	private static List<MenuRolesEntity>GetCacheList(){
		if(Cache==null)return default(List<MenuRolesEntity>);
		List<MenuRolesEntity>ls= Cache.Get<List<MenuRolesEntity>>(CACHE_KEY_LS);
		if(ls==null){
			ls = new List<MenuRolesEntity>();
			AddCache(CACHE_KEY_LS,ls);
		}
		return ls;
	}
	public List<MenuRolesEntity>ListMenuRoles{
		get{
			return LoadAllMenuRoles(false);
		}
	}
	public static List<MenuRolesEntity>ListMenuRolesStatic{
		get{
			LoadAllMenuRoles(false);
			return _ListMenuRoles;
		}
	}
	public MenuRolesEntity FindMenuRoles(Int32 MenuRoleId){
		if(AllowCaching){
			_ListMenuRoles=GetCacheList();
		}		
		MenuRolesEntity oResult=_ListMenuRoles.Find(delegate(MenuRolesEntity oFind){
			bool result=true; 
			result&=oFind.MenuRoleId.Equals(MenuRoleId);
			return result;
		 });
		return oResult;
	}
	public static List<MenuRolesEntity>LoadAllMenuRoles(bool clearCache){
		_ListMenuRoles = new List<MenuRolesEntity>();
		if(AllowCaching){
			_ListMenuRoles=GetCacheList();
		}
		if(_ListMenuRoles==null||_ListMenuRoles.Count==0)
			clearCache=true;
		if(!clearCache)return _ListMenuRoles;
		_ListMenuRoles=new List<MenuRolesEntity>(Service.SelectAll());		
		if(AllowCaching)
			AddCache(CACHE_KEY_LS,_ListMenuRoles);
		return _ListMenuRoles;
	}
	#endregion
}
}
